package com.alibaba.travel.scenic.service.impl;

import com.alibaba.travel.common.constant.RedisConstants;
import com.alibaba.travel.common.utils.*;
import com.alibaba.travel.scenic.dao.UserScenicHistoryDao;
import com.alibaba.travel.scenic.entity.ScenicSpotEntity;
import com.alibaba.travel.scenic.entity.UserScenicHistoryEntity;
import com.alibaba.travel.scenic.service.ScenicSpotService;
import com.alibaba.travel.scenic.service.UserScenicHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.travel.common.utils.Query;
import com.alibaba.travel.common.utils.PageUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("userScenicHistoryService")
public class UserScenicHistoryServiceImpl extends ServiceImpl<UserScenicHistoryDao, UserScenicHistoryEntity> implements UserScenicHistoryService {

    @Resource
    private ScenicSpotService scenicSpotService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ObjectMapper objectMapper;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserScenicHistoryEntity> page = this.page(
                new Query<UserScenicHistoryEntity>().getPage(params),
                new QueryWrapper<UserScenicHistoryEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 分页查询用户历史旅游景点
     * @param id
     * @return
     */
    @Override
    public R pageSelect(Long id,Integer page,Integer pageSize) {
        //校验
        if(ObjectUtils.isEmpty(id)){
            return R.error("参数为空!");
        }

//        先查询缓存是否有对应的景点，根据用户id查询   TODO 注意缓存更新
//        String json = stringRedisTemplate.opsForValue().get(RedisConstants.SCENIC_HIS_SPOT_KEY + id);
//        String json = null;
//        do {
//             json = stringRedisTemplate.opsForList().leftPop(RedisConstants.SCENIC_HIS_SPOT_KEY + id);
//             if(!StringUtils.isEmpty(json)){
//                 try {
//                     ScenicSpotEntity scenicSpotEntity = objectMapper.readValue(json, ScenicSpotEntity.class);
//                 } catch (JsonProcessingException e) {
//                     return R.error(e.getMessage());
//                 }
//             }
//        }while (StringUtils.isEmpty(json));

        //不存在，查询数据库并且更新缓存
        //根据用户id查询所有的历史景点榜单的id集合。
        Page<ScenicSpotEntity> targetPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<UserScenicHistoryEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserScenicHistoryEntity::getUserId,id);
        List<UserScenicHistoryEntity> list = baseMapper.selectList(wrapper);
        List<Long> collect = list.stream().map((item) -> {
            String scenicId = item.getScenicId();
            return Long.valueOf(scenicId);
        }).distinct().collect(Collectors.toList());

        //获取id集合后，查询景点信息。
        LambdaQueryWrapper<ScenicSpotEntity> spotWr = new LambdaQueryWrapper<>();
        collect.stream().forEach((item)->{
            spotWr.eq(ScenicSpotEntity::getId,item);
        });
        scenicSpotService.getBaseMapper().selectPage(targetPage,spotWr);

        return R.ok("查询成功").setData(targetPage);
    }

}